package com.ryl.yomall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ryl.yomall.model.product.*;
import com.ryl.yomall.product.mapper.*;
import com.ryl.yomall.product.service.ManageService;
import com.ryl.yomall.rabbitmq.constant.MQConstant;
import com.ryl.yomall.rabbitmq.service.RabbitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author 我的世界
 */
@Service
public class ManageServiceImpl implements ManageService {
    @Autowired
    private BaseCategory1Mapper baseCategory1Mapper;

    @Autowired
    private BaseCategory2Mapper baseCategory2Mapper;

    @Autowired
    private BaseCategory3Mapper baseCategory3Mapper;

    @Autowired
    private BaseAttrInfoMapper baseAttrInfoMapper;

    @Autowired
    private BaseAttrValueMapper baseAttrValueMapper;

    @Autowired
    private BaseTrademarkMapper baseTrademarkMapper;

    @Autowired
    private SpuInfoMapper spuInfoMapper;

    @Autowired
    private BaseSaleAttrMapper baseSaleAttrMapper;

    @Autowired
    private SpuImageMapper spuImageMapper;

    @Autowired
    private SpuSaleAttrMapper spuSaleAttrMapper;

    @Autowired
    private SpuSaleAttrValueMapper spuSaleAttrValueMapper;

    @Autowired
    private SkuInfoMapper skuInfoMapper;

    @Autowired
    private SkuImageMapper skuImageMapper;

    @Autowired
    private SkuAttrValueMapper skuAttrValueMapper;

    @Autowired
    private SkuSaleAttrValueMapper skuSaleAttrValueMapper;
    @Autowired
    private RabbitService rabbitService;



    @Override
    public List<BaseCategory1> getCategory1() {
        return baseCategory1Mapper.selectList(null);
    }

    @Override
    public List<BaseCategory2> getCategory2(Long category1Id) {
        // select * from baseCategory2 where Category1Id = ?
        QueryWrapper queryWrapper = new QueryWrapper<BaseCategory2>();
        queryWrapper.eq("category1_id",category1Id);
        List<BaseCategory2> baseCategory2List = baseCategory2Mapper.selectList(queryWrapper);
        return baseCategory2List;
    }

    @Override
    public List<BaseCategory3> getCategory3(Long category2Id) {
        // select * from baseCategory3 where Category2Id = ?
        QueryWrapper queryWrapper = new QueryWrapper<BaseCategory3>();
        queryWrapper.eq("category2_id",category2Id);
        return baseCategory3Mapper.selectList(queryWrapper);
    }

    @Override
    public List<BaseAttrInfo> getAttrInfoList(Long category1Id,Long category2Id,Long category3Id) {
        // 调用mapper：
        return baseAttrInfoMapper.selectBaseAttrInfoList(category1Id, category2Id, category3Id);
    }

    @Override
    public void baseTrademarkDelete(Long id) {
        baseTrademarkMapper.deleteById ( id );
    }

    /**
     * 商品分页列表
     */
    @Override
    public IPage<SpuInfo> spuList(Integer page, Integer limit, Long category3Id) {
        return spuInfoMapper.selectPage(new Page<SpuInfo>(page,limit),
                new QueryWrapper<SpuInfo>().eq("category3_id",category3Id));

    }

    /**
     * 查询所有品牌,不分页
     */
    @Override
    public List<BaseTrademark> getTrademarkList() {

        return baseTrademarkMapper.selectList ( null );
    }

    @Override
    public List<BaseSaleAttr> baseSaleAttrList() {

        return baseSaleAttrMapper.selectList ( null );

    }

    /**
     * 添加SPU
     */
    @Override
    public void saveSpuInfo(SpuInfo spuInfo) {
     // 1.spu_info
        spuInfoMapper.insert ( spuInfo );
     // 2. spu_img
        List<SpuImage> spuImageList = spuInfo.getSpuImageList ( );
        spuImageList.forEach ( spuImage -> {
            //外键
            spuImage.setSpuId ( spuInfo.getId () );
            spuImageMapper.insert ( spuImage );

        } );
        // 3.spu_sale_attr
        List<SpuSaleAttr> spuSaleAttrList = spuInfo.getSpuSaleAttrList ( );
        spuSaleAttrList.forEach ( spuSaleAttr -> {
            spuSaleAttr.setSpuId ( spuInfo.getId () );
            spuSaleAttrMapper.insert ( spuSaleAttr );
            // 4.spu_sale_value
            List<SpuSaleAttrValue> spuSaleAttrValueList = spuSaleAttr.getSpuSaleAttrValueList ( );
            spuSaleAttrValueList.forEach ( spuSaleAttrValue -> {
                //外键
                spuSaleAttrValue.setSpuId ( spuInfo.getId () );
                //销售属性名称
                spuSaleAttrValue.setSaleAttrName ( spuSaleAttr.getSaleAttrName () );

                spuSaleAttrValueMapper.insert(spuSaleAttrValue);

            } );

        } );


    }

    @Override
    public List<SpuImage> spuImageList(Long spuId) {
        QueryWrapper<SpuImage> queryWrapper=new QueryWrapper<> (  );
        queryWrapper.eq ( "spu_id",spuId );
        return spuImageMapper.selectList ( queryWrapper );
    }

    /**
     * 根据商品ID查询 销售属性及属性值集合
     */
    @Override
    public List<SpuSaleAttr> spuSaleAttrList(Long spuId) {
        List<SpuSaleAttrValue> spuSaleAttrValues = spuSaleAttrValueMapper.spuSaleAttrList ( spuId );
        List<SpuSaleAttr> list=new ArrayList<> (  );
        Set<String> set=new HashSet<> (  );
        for (SpuSaleAttrValue spuSaleAttrValue : spuSaleAttrValues) {
            set.add ( spuSaleAttrValue.getSaleAttrName () );
        }
            for (String set1 : set) {
                SpuSaleAttr spuSaleAttr=new SpuSaleAttr ();
                spuSaleAttr.setSaleAttrName ( set1 );
                List<SpuSaleAttrValue> list1=new ArrayList<> (  );
                for (SpuSaleAttrValue spuSaleAttrValue : spuSaleAttrValues) {

                    if(spuSaleAttr.getSaleAttrName ().equals ( spuSaleAttrValue.getSaleAttrName () ))
                   {
                      list1.add ( spuSaleAttrValue );
                   }
            }
                spuSaleAttr.setSpuSaleAttrValueList ( list1 );
                list.add ( spuSaleAttr );

        }

        return list;
        //return spuSaleAttrMapper.spuSaleAttrList ( spuId );
    }

    /**
     * 保存SKU
     */
    @Override
    public void saveSkuInfo(SkuInfo skuInfo) {
        //1:sku_info  1
        skuInfoMapper.insert(skuInfo);
        //2:sku_image N
        List<SkuImage> skuImageList = skuInfo.getSkuImageList();
        skuImageList.forEach(skuImage -> {
            skuImage.setSkuId(skuInfo.getId());
            skuImageMapper.insert(skuImage);
        });
        //3:sku_attr_value N
        List<SkuAttrValue> skuAttrValueList = skuInfo.getSkuAttrValueList();
        skuAttrValueList.forEach(skuAttrValue -> {
            skuAttrValue.setSkuId(skuInfo.getId());
            skuAttrValueMapper.insert(skuAttrValue);
        });
        //4:sku_sale_attr_value N
        List<SkuSaleAttrValue> skuSaleAttrValueList = skuInfo.getSkuSaleAttrValueList();
        skuSaleAttrValueList.forEach(skuSaleAttrValue -> {
            // 库存表的外键
            skuSaleAttrValue.setSkuId(skuInfo.getId());
            //商品表的外键
            skuSaleAttrValue.setSpuId(skuInfo.getSpuId ( ));
            skuSaleAttrValueMapper.insert(skuSaleAttrValue);
        });
    }



    /**
     * 删除属性值
     */
    @Override
    public void deleteAttrValue(String attrValueName) {
        QueryWrapper<BaseAttrValue> queryWrapper=new QueryWrapper<> (  );
        queryWrapper.eq ( "value_name",attrValueName );
        baseAttrValueMapper.delete ( queryWrapper );
    }

    /**
     * 查询SKU分页列表
     */
    @Override
    public IPage<SkuInfo> list(Integer page ,Integer limit) {

        return skuInfoMapper.selectPage ( new Page<> ( page,limit ),null );

    }

    /**
     * 上架
     */
    @Override
    public void onSale(Long skuId) {
        SkuInfo skuInfo=new SkuInfo ();
        skuInfo.setId ( skuId );
        //配置全局 常量
        skuInfo.setIsSale ( SkuInfo.IS_SALE );
        skuInfoMapper.updateById ( skuInfo );
        // 发消息给MQ,通知其他微服务
        rabbitService.sendMessage ( MQConstant.EXCHANGE_DIRECT_GOODS,MQConstant.ROUTING_GOODS_UPPER
                ,skuId);
    }

    /**
     * 下架
     */
    @Override
    public void cancelSale(Long skuId) {
        SkuInfo skuInfo=new SkuInfo ();
        skuInfo.setId ( skuId );
        skuInfo.setIsSale ( SkuInfo.CANCEL_SALE );
        skuInfoMapper.updateById ( skuInfo );
        //发消息给MQ,通知其他微服务
        rabbitService.sendMessage ( MQConstant.EXCHANGE_DIRECT_GOODS,MQConstant.ROUTING_GOODS_LOWER
                ,skuId);
    }


    @Override
    public void baseTrademarkAdd(BaseTrademark trademark) {
        baseTrademarkMapper.insert (trademark);
    }

    @Override
    public void baseTrademarkUpdate(BaseTrademark trademark) {
        baseTrademarkMapper.updateById(trademark);
    }

    @Override
    public BaseTrademark getBaseTrademark(Long id) {
        return baseTrademarkMapper.selectById(id);
    }

    /**
     * 查询品牌分页列表
     */
    @Override
    public IPage<BaseTrademark> baseTrademark(Integer page, Integer limit) {

        //select * from base_trademark where .. limit page,limit
        return baseTrademarkMapper.selectPage(new Page<BaseTrademark>(page,limit),
                null);
    }

    @Override
    public void deleteAttrInfo(String attrId) {
        //1.先删除属性值
        QueryWrapper<BaseAttrValue> wrapper=new QueryWrapper<> (  );
        wrapper.eq ( "attr_id",attrId );
        baseAttrValueMapper.delete ( wrapper );
        //2.再删除属性
        baseAttrInfoMapper.deleteById ( attrId );

    }



    @Override
    public void saveAttrInfo(BaseAttrInfo baseAttrInfo) {
        QueryWrapper<BaseAttrInfo> queryWrapper =new QueryWrapper<> (  );
        queryWrapper.eq ( "attr_name", baseAttrInfo.getAttrName ());
        Integer integer = baseAttrInfoMapper.selectCount ( queryWrapper );
        //1. 平台属性表
        if(integer==null || integer==0){
            baseAttrInfoMapper.insert ( baseAttrInfo );
            //2.平台属性值表
            List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList ( );
            attrValueList.forEach ( attrValue ->{
                attrValue.setAttrId ( baseAttrInfo.getId () );
                baseAttrValueMapper.insert ( attrValue );
            } );
        }
        baseAttrInfoMapper.updateById ( baseAttrInfo );
        //2.平台属性值表
        List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList ( );
        attrValueList.forEach ( attrValue ->{
            QueryWrapper<BaseAttrValue> queryWrapper1=new QueryWrapper<> (  );
            queryWrapper1.eq ( "value_name",attrValue.getValueName () );
            Integer integer1 = baseAttrValueMapper.selectCount ( queryWrapper1 );
            if(integer1 == null || integer1==0){
                attrValue.setAttrId ( baseAttrInfo.getId () );
                baseAttrValueMapper.insert ( attrValue );
            }
        } );

    }

    @Override
    public BaseAttrInfo getAttrInfo(Long attrId) {
        BaseAttrInfo baseAttrInfo = baseAttrInfoMapper.selectById(attrId);
        // 查询到最新的平台属性值集合数据放入平台属性中！
        baseAttrInfo.setAttrValueList(getAttrValueList(attrId));
        return baseAttrInfo;
    }



    /**
     * 根据属性id获取属性值
     * @param attrId
     * @return
     */
    private List<BaseAttrValue> getAttrValueList(Long attrId) {
        // select * from baseAttrValue where attrId = ?
        QueryWrapper queryWrapper = new QueryWrapper<BaseAttrValue>();
        queryWrapper.eq("attr_id", attrId);
        List<BaseAttrValue> baseAttrValueList = baseAttrValueMapper.selectList(queryWrapper);
        return baseAttrValueList;
    }

}
